<template>
	<div>
		<div style="padding: 20px;width: 100%;height: 100%;">
			<div style="float: right;margin-top: -15px;">
				<Button class="row-btn" style="margin-right: 10px; height: 32px; line-height: 32px; background-color: #ececec;"
				 type="text" @click="Resume">
					<Icon type="ios-skip-forward" size="18" />恢复所有下载
				</Button>
			</div>
		</div>
		<div style="padding: 10px">
			<Table border :columns="columns12" :data="data" :show-header="false" :showBorder="false" highlight-row>
				<template slot-scope="{ row }" slot="name">
					<div style="height: 10px;"></div>
					<Icon type="ios-albums" size="20" style="float: left; padding-right: 10px;" />
					<h3>{{ (row.files[0].path).substring((row.files[0].path).lastIndexOf('/') + 1, (row.files[0].path).length) }}</h3>
					<strong></strong>
					<Progress :percent="(Number((row.completedLength / row.totalLength * 100).toFixed(2)))" />
					<div style="margin-top: 10px;">
						<h4 style="float: left;">
							当前完成进度：{{(row.completedLength / 1000000).toFixed(2)}} MB /
							{{(row.totalLength / 1000000).toFixed(2)}}
							MB
						</h4>
						<h4 style="float: right;">当前状态：已暂停</h4>
					</div>
					<div style="height: 20px;"></div>
				</template>
				<template slot-scope="{ row, index }" slot="action">
					<ButtonGroup shape="circle">
						<Button icon="md-code-download" @click="recovery(index)"></Button>
						<Button icon="md-link" @click="copyLink(index)"></Button>
						<Button icon="md-alert" @click="details(index)"></Button>
						<Button icon="md-close" @click="Stop(index)"></Button>
					</ButtonGroup>
				</template>
			</Table>
		</div>
	</div>
</template>
<script>
	import axios from "axios";
	export default {
		data() {
			return {
				columns12: [{
						title: "Name",
						slot: "name"
					},
					{
						title: "Action",
						slot: "action",
						width: 200,
						align: "center"
					}
				],
				data: []
			};
		},
		mounted() {
			this.fjhv();
		},
		methods: {
			remove(index) {
				console.log(this.data6[index].age);
			},
			fjhv() {
				var url = "http://127.0.0.1:6800/jsonrpc";
				var json_rpc1 = {
					id: "",
					jsonrpc: "2.0",
					method: "aria2.tellWaiting",
					params: ["token:kumao", 0, 5]
				};

				var interval =  setInterval((res => {
					if(this.$route.path == '/Paused' ){
					axios({
						url: url,
						method: 'post',
						crossDomain: true,
						processData: false,
						data: JSON.stringify(json_rpc1),
						headers: {
							contentType: 'application/json'
						}
					}).then((respanse) => {
						var _this = this
						this.data = respanse.data.result
					})
					}else{
						clearInterval(interval);
					}

				}), 2000);
			},
			//恢复所有下载
			Resume() {
				console.log(this.data.gid)
				console.log(this.data.status)
				for (var i = 0; i < this.data.length; i++) {
					var url = "http://127.0.0.1:6800/jsonrpc";

					axios({
						url: url,
						method: "post",
						crossDomain: true,
						processData: false,
						data: JSON.stringify({
							id: "",
							jsonrpc: "2.0",
							method: "aria2.unpause",
							params: [this.data[i].gid]
						}),
						headers: {
							contentType: "application/json"
						}
					}).then(respanse => {
						console.log(respanse.data)
					});
				}
			},
			// 打开下载文件的目录
			// folder(index) {
			// 	var exec = require("child_process").exec;
			// 	exec(
			// 		"start " +
			// 		this.data[index].dir
			// 		.substring(
			// 			this.data[index].dir.indexOf("/") + 1,
			// 			this.data[index].dir.length
			// 		)
			// 		.split("/")
			// 		.join("\\")
			// 	);
			// },
			//单个下载
			recovery(index) {
				console.log(this.data.gid)
				console.log(this.data.status)
				for (var i = 0; i < this.data.length;) {
					var url = "http://127.0.0.1:6800/jsonrpc";

					axios({
						url: url,
						method: "post",
						crossDomain: true,
						processData: false,
						data: JSON.stringify({
							id: "",
							jsonrpc: "2.0",
							method: "aria2.unpause",
							params: [this.data[i].gid]
						}),
						headers: {
							contentType: "application/json"
						}
					}).then(respanse => {
						console.log(respanse.data)
					});
				}
			},

			//关闭下载文件
			Stop(index) {
				var url = 'http://127.0.0.1:6800/jsonrpc'
				var json_rpc1 = {
					id: '',
					jsonrpc: '2.0',
					method: 'aria2.remove',
					params: [this.data[index].gid]
				};

				axios({
					url: url,
					method: 'post',
					crossDomain: true,
					processData: false,
					data: JSON.stringify(json_rpc1),
					headers: {
						contentType: 'application/json'
					}
				}).then((respanse) => {})
			},

			//复制下载连接
			copyLink(index) {
				this.$copyText(this.data[index].files[0].uris[0].uri).then((res) => {
					this.$Message.success('复制下载链接成功,可以分享给你的朋友啦！')
				});
			},
			// 下载详细
			details(index) {
				this.$Modal.info({
					title: '详细信息：' + this.data[index].files[0].path.substring(this.data[index].files[0].path.lastIndexOf("/") + 1,
						this.data[index].files[0].path.length),
					content: '<strong>GID：' + this.data[index].gid + '<br/>' +
						'<strong>已完成的下载：' + (this.data[index].completedLength / 1000000).toFixed(2) + 'MB' + '<br/>' +
						'<strong>文件总量大小：' + (this.data[index].totalLength / 1000000).toFixed(2) + 'MB' + '<br/>' +
						'<strong>文件保存目录：' + this.data[index].dir + '<br/>'

				});
			}
		}
	};
</script>
